package cn.sumpu.app.coolsk.biz.impl;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
//import org.slf4j.*;
import cn.sumpu.app.coolsk.biz.*;
import cn.sumpu.app.coolsk.dao.*;
import cn.sumpu.app.coolsk.domain.*;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.toolkit.common.page.PageModel;
import cn.sumpu.toolkit.common.util.Common;
public class MessageBizImpl implements MessageBiz {

//	private final static Logger logger = LoggerFactory
//			.getLogger(InfoBizImpl.class);

	@Autowired
	private MemberBiz memberBiz;
	
	private MessageDao messageDao;

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	//批量发送站内信
	@Override
	public String addMessages(String allPost, String nicknames, String content)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sender", "100000");
		map.put("content", content);
		if ("all".equals(allPost)) {
			map.put("allPost", "all");
			messageDao.addMessages(map);
			return "success";
		} else {
			String str = "";
			List<Message> messages = new ArrayList<Message>();
			Message m = null;
			Member member = null;
			String[] nicknameArr = nicknames.split(",");
			for (String n : nicknameArr) {
				member = memberBiz.getMemberNickName(n);
				if(member == null){
					str += n + ",";
				}else {
					m = new Message("1", member.getId(), content);
					messages.add(m);
				}
			}
			map.put("list", messages);
			if(messages.size()==0){
				str = "allWrong";
			}else if("".equals(str)){
				messageDao.addMessages(map);
				str = "success";
			}else if(",".equals(str.substring(str.length()-1, str.length()))){
				str = str.substring(0, str.length()-1);
			}
			return str;
		}
	}
	//删除站内信
	@Override
	public void delMessage(String id) throws AppBusinessException {
		if(Common.emptyStr(id)){
			return;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		String[] ids = id.split("_");
		for(int i=0; i<ids.length; i++){
			map.put("id", ids[i]);
			messageDao.delMessage(map);
		}
	}
	//查询站内信
	@Override
	public PageModel getMessages(String addressee, Integer page)
			throws AppBusinessException {
		int pageSize = 10;
		if(page==null || page<1){
			page = 1;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("addressee", addressee);
		map.put("pageSize", pageSize);
		map.put("startId", (page - 1) * pageSize);
		List<Message> messageList = messageDao.getMessages(map);
		Integer count = getMessageCount(addressee);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(messageList);
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pageSize - 1) / pageSize);
		return pm;
	}
	//查询站内信总条数
	@Override
	public Integer getMessageCount(String addressee)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("addressee", addressee);
		return messageDao.getMessageCount(map);
	}
	
}
